package com.example.chartroom1.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;


import java.util.concurrent.ConcurrentHashMap;

@Component
@Slf4j
public class OnlineManage {
    private ConcurrentHashMap<Integer, WebSocketSession> map
            = new ConcurrentHashMap<>();

    public void online(int userId, WebSocketSession session){
        if(map.get(userId) != null){
            log.info("该用户已经被登录");
            return;
        }
        map.put(userId, session);
    }

    public void offline(int userId, WebSocketSession session){
        if(map.get(userId) == session){
            map.remove(userId);
        }
    }

    public WebSocketSession getSession(int userId){
        return map.get(userId);
    }
}
